<?php
/**
 * Created by PhpStorm.
 * User: liujun
 * Date: 2018/8/6
 * Time: 上午9:52
 */

namespace PddWechat\Library;


use Dai\Framework\Library\HttpProxy;
use Dai\Framework\Library\Log;

class PddLibrary
{
    CONST PDD_REFER = "http://jinbao.pinduoduo.com";
    CONST TIMEOUT = 100;
    CONST COOKIE_PATH = APP_PATH."/var/ddjb.tmp";

    private static function getHeader($dataString, $cookie=null) {
        $headers = [
            'Content-Type: application/json',
            'Content-Length: ' . strlen($dataString),
        ];

        if( $cookie != null) {
            $headers[] = $cookie;
        }
        return $headers;
    }

    public static function login(){
        //普通数组也行
        $url = "http://jinbao.pinduoduo.com/network/api/account/loginPassword";

        $data = [
            'mobile' => "15658177153",
            'password' => "124567as",
        ];
        $dataString =  json_encode($data);
        $headers = self::getHeader($dataString);
        $res = HttpProxy::call($url, $dataString, $headers, self::TIMEOUT,  self::PDD_REFER, null, self::COOKIE_PATH);
        echo  "登录".var_dump($res);
    }

    public static function addGoodsUrl($goodsId, $inviteCode){
        $url = "http://jinbao.pinduoduo.com/network/api/promotion/createPromotionUrl";
        $data = [
            'goodsId' => $goodsId,
            'customParameters' => $inviteCode,
            'pid' => "1884871_22329699",
        ];

        $res = self::doHttp($url, self::PDD_REFER, $data);
        $res = json_decode($res, true);
        if( $res['success'] !== true) {
            self::login();
            $res = self::doHttp($url, self::PDD_REFER, $data);
            $res = json_decode($res, true);
        }

        if( $res['success'] !== true ) {
            Log::error(sprintf("get url %s fail, data %s, result %s", $url, var_export($data, true), var_export($res, true)));
            return false;
        }
        return $res['result']['weAppWebViewUrl'];
    }


    public static function addMallUrl($openId, $inviteCode) {
        $url = "http://jinbao.pinduoduo.com/network/api/mall/url";
        $data = [
            'customParameters' => $inviteCode,
            'pidList' => ["1884871_22329699"],
        ];

        $res = self::doHttp($url, self::PDD_REFER, $data);
        $res = json_decode($res, true);
        if( $res['success'] !== true) {
            self::login();
            $res = self::doHttp($url, self::PDD_REFER, $data);
            $res = json_decode($res, true);
        }

        if( $res['success'] !== true || $res['result']['total'] != 1 ) {
            Log::error(sprintf("get url %s fail, data %s, result %s", $url, var_export($data, true), var_export($res, true)));
            return false;
        }
        return $res['result']['urlList'][0]['url'];
    }

    public static function addRedPackUrl($openId, $inviteCode){
        $url = "http://jinbao.pinduoduo.com/network/api/promotion/redPacket/createUrl";
        $data = [
            'customParameters' => $inviteCode,
            'pid' => "1884871_22329699",
        ];

        $res = self::doHttp($url, self::PDD_REFER, $data);
        $res = json_decode($res, true);
        if( $res['success'] !== true) {
            self::login();
            $res = self::doHttp($url, self::PDD_REFER, $data);
            $res = json_decode($res, true);
        }
        if( $res['success'] !== true  ) {
            Log::error(sprintf("get url %s fail, data %s, result %s", $url, var_export($data, true), var_export($res, true)));
            return false;
        }
        return $res['result']['url'];
    }

    /**
     * @param $goodsId
     * @return bool|mixed
     */
    public static function getGoodsInfo($goodsId) {
        $url = "http://jinbao.pinduoduo.com/network/api/common/goodsList";
        $data = [
            'keyword' => $goodsId,
            'pageNumber' => 1,
            'pageSize' => 60,
            'sortType' => 0,
        ];

        $res = self::doHttp($url, self::PDD_REFER, $data);
        $res = json_decode($res, true);
        if( $res['success'] !== true) {
            return false;
        }
        return $res['result']['goodsList'][0];
    }

    public static function getOrderList($pageNum, $pageSize, $beginTime, $endTime) {
        $url = "http://jinbao.pinduoduo.com/network/api/order/list";
        $data = [
            'pageNum' => $pageNum,
            'pageSize' => $pageSize,
            'timeType' => 9,
            'startTime' => $beginTime,
            'endTime' => $endTime,
        ];
        $refer = "http://jinbao.pinduoduo.com/data/order-detail";

        $res = self::doHttp($url, $refer, $data);
        $res = json_decode($res, true);

        if( $res['success'] !== true) {
            self::login();
            $res = self::doHttp($url, self::PDD_REFER, $data);
            $res = json_decode($res, true);
        }

        return $res;
    }

    public static function doHttp($url, $refer, $data) {
        $dataString =  json_encode($data);
        $headers = self::getHeader($dataString);
        $res = HttpProxy::call($url, $dataString, $headers, self::TIMEOUT,  $refer, self::COOKIE_PATH, null);
        Log::info(sprintf("get from url [%s], data [%s], result [%s]", $url, json_encode($data), json_encode($res)));
        return $res;
    }
}